/**
 * @license
 * PXT Blockly
 *
 * Copyright (c) Microsoft Corporation. All rights reserved.
 * https://github.com/Microsoft/pxt-blockly
 *
 * See LICENSE file for details.
 */

/**
 * @fileoverview Block extensions.
 */

'use strict';
goog.provide('Blockly.PXTBlockly.Extensions');

goog.require('Blockly.Colours');
goog.require('Blockly.constants');

Blockly.PXTBlockly.Extensions.INLINE_SVGS = function() {
  this.ADD_IMAGE_DATAURI = '';
  this.REMOVE_IMAGE_DATAURI = '';
};

/**
 * pxt-blockly
 * Mixin to add a "edit" option to the context menu of a function call or
 * definition block.
 * @mixin
 * @augments Blockly.Block
 * @package
 * @readonly
 */
Blockly.PXTBlockly.Extensions.FUNCTION_CONTEXTMENU_EDIT = {
  /**
   * Add the "edit" option to the context menu.
   * @param {!Array.<!Object>} menuOptions List of menu options to edit.
   * @this Blockly.Block
   */
  customContextMenu: function(menuOptions) {
    menuOptions.push(Blockly.Functions.makeEditOption(this));

    // Find and remove the duplicate option for definitions
    if (this.type == Blockly.FUNCTION_DEFINITION_BLOCK_TYPE) {
      for (var i = 0, option; option = menuOptions[i]; i++) {
        if (option.text == Blockly.Msg.DUPLICATE_BLOCK) {
          menuOptions.splice(i, 1);
          break;
        }
      }
    }
  }
};

/**
 * pxt-blockly
 * Extension to represent a number reporter. That means the block has inline
 * inputs, a round output shape, and a 'Number' output type.
 * @this {Blockly.Block}
 * @readonly
 */
Blockly.PXTBlockly.Extensions.OUTPUT_NUMBER = function() {
  this.setInputsInline(true);
  this.setOutputShape(Blockly.OUTPUT_SHAPE_ROUND);
  this.setOutput(true, 'Number');
};

/**
 * pxt-blockly
 * Extension to represent a string reporter. That means the block has inline
 * inputs, a round output shape, and a 'String' output type.
 * @this {Blockly.Block}
 * @readonly
 */
Blockly.PXTBlockly.Extensions.OUTPUT_STRING = function() {
  this.setInputsInline(true);
  this.setOutputShape(Blockly.OUTPUT_SHAPE_ROUND);
  this.setOutput(true, 'String');
};

/**
 * pxt-blockly
 * Extension to represent a boolean reporter. That means the block has inline
 * inputs, a hexagonal output shape, and a 'Boolean' output type.
 * @this {Blockly.Block}
 * @readonly
 */
Blockly.PXTBlockly.Extensions.OUTPUT_BOOLEAN = function() {
  this.setInputsInline(true);
  this.setOutputShape(Blockly.OUTPUT_SHAPE_HEXAGONAL);
  this.setOutput(true, 'Boolean');
};

Blockly.PXTBlockly.Extensions.registerAll = function() {
  Blockly.Extensions.register('inline-svgs', this.INLINE_SVGS);

  // pxt-blockly: Register functions-related extensions
  Blockly.Extensions.registerMixin('function_contextmenu_edit',
      Blockly.PXTBlockly.Extensions.FUNCTION_CONTEXTMENU_EDIT);
  Blockly.Extensions.register('output_number', Blockly.PXTBlockly.Extensions.OUTPUT_NUMBER);
  Blockly.Extensions.register('output_string', Blockly.PXTBlockly.Extensions.OUTPUT_STRING);
  Blockly.Extensions.register('output_boolean', Blockly.PXTBlockly.Extensions.OUTPUT_BOOLEAN);
};

Blockly.PXTBlockly.Extensions.registerAll();
